package com.huiyujinrong.qiangdanbao.activity.login;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.activity.ActivityFragment;
import com.huiyujinrong.qiangdanbao.utils.DataUtils;
import com.huiyujinrong.qiangdanbao.utils.SharedHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity implements LoginContract.View {

    @BindView(R.id.lg_tell)
    EditText tell;
    @BindView(R.id.lg_pwd)
    EditText pwd;

    private long exitTime = 0;

    private LoginPresenter presenter;

    private Toast t;

    private ProgressDialog progressDialog;

    private boolean isBack = false;//是否需要返回



    @BindView(R.id.toolbar_Title)
    TextView toolbar_title;
    @BindView(R.id.toolbar_Back)
    RelativeLayout toolbar_back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        setToolbar();

        presenter = new LoginPresenter(this, this);
        presenter.start();

        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        isBack = getIntent().getBooleanExtra("isBack", false);

        inToLogin();

    }


    /**
     * 设置toolbar
     */
    private void setToolbar(){
        toolbar_title.setText("登录");
        toolbar_back.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.toolbar_Back)
    void goBack(){
        finish();
    }


    /**
     * 自动登录
     */
    private void inToLogin() {
        String s_tel = String.valueOf(SharedHelper.get(this, "tel", ""));
        String s_pwd = String.valueOf(SharedHelper.get(this, "pwd", ""));

        BorrowHelpApp.showLOG(s_tel);
        BorrowHelpApp.showLOG(s_pwd);
    }


    //  登录
    @OnClick(R.id.lg_login)
    void inLogin() {
        if (DataUtils.getCheckTel(tell.getText().toString())) {
            if (TextUtils.isEmpty(pwd.getText().toString()) && pwd.getText().length() < 6) {
                showToast("密码必须大于6位");
            } else {
                SharedHelper.put(this, "tel", tell.getText().toString());
                SharedHelper.put(this, "pwd", pwd.getText().toString());
                presenter.login(tell.getText().toString(), pwd.getText().toString());
            }
        } else {
            showToast("手机号有误");
        }
    }

    //  忘记密码
    @OnClick(R.id.lg_forgetPwd)
    void inForgetPwd() {
        startActivity(new Intent(this, ForgetActivity.class));
    }

    //  信贷经理注册
    @OnClick(R.id.lg_upRegister)
    void inUpRegister() {
        startActivity(new Intent(this, RegisterActivity.class));

    }


    //2次连续点击 退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if (isBack) {
            setResult(RESULT_OK);
            finish();
        } else {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
        }
    }

    @Override
    public void showToast(String str) {
        if (t == null) {
            t = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        } else {
            t.setText(str);
            t.setDuration(Toast.LENGTH_SHORT);
        }
        t.show();
    }

    @Override
    public void exits() {
        startActivity(new Intent(this, ActivityFragment.class));
        SharedHelper.put(this, "isLogin", true);
        BorrowHelpApp.setTel(this,tell.getText().toString());
        BorrowHelpApp.setPwd(this,pwd.getText().toString());
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.end();
    }

    @Override
    public void disPlay() {
        progressDialog.setMessage("正在登陆，请稍后");
        progressDialog.show();
    }

    @Override
    public void disMiss() {
        progressDialog.dismiss();
    }
}
